cap log close
clear all
cap log close
set more off

* Diretório principal
cd "/Stata_moments_for_Matlab/bootstrap"

* Demais caminhos
global origdata "/Stata_moments_for_Matlab/bootstrap"
global savedata "/Stata_moments_for_Matlab/bootstrap"
global datamun  "/Stata_moments_for_Matlab/bootstrap"
global matlab 	"/Stata_moments_for_Matlab/bootstrap"


local i = 0
local J = 1
local B = 100
while `J' <= `B' {
use "$savedata/sample_formodel", clear 
bsample, cluster(municip)
ge iter =`J'


save bootsample1, replace


foreach yt in  1 2  {					
			foreach ed in 0 1  { 
				foreach comp in 0 1  {			/*with children in hh or not*/
					preserve
					di "educationhigh `ed' with children `comp' period `yt'"
					keep if dchild0_14 == `comp' &  educationhigh == `ed' & period == `yt' 
					
												
					* Wages - first interview
										
					use if dchild0_14 == `comp' &  educationhigh == `ed' & period == `yt' using bootsample1, clear												
					collapse (mean) mean_wi_1=wi_1 mean_wf_1=wf_1 mean_wi_2=wi_2 mean_wf_2=wf_2 (sd) sd_wi_1=wi_1 sd_wf_1=wf_1 sd_wi_2=wi_2 sd_wf_2=wf_2 /*
					*/ (p10) p10_wi_1=wi_1 p10_wf_1=wf_1 p10_wi_2=wi_2 p10_wf_2=wf_2 (p25) p25_wi_1=wi_1 p25_wf_1=wf_1 p25_wi_2=wi_2 p25_wf_2=wf_2 /*
					*/ (p50) p50_wi_1=wi_1 p50_wf_1=wf_1 p50_wi_2=wi_2 p50_wf_2=wf_2 (p75) p75_wi_1=wi_1 p75_wf_1=wf_1 p75_wi_2=wi_2 p75_wf_2=wf_2 /*
					*/ (p90) p90_wi_1=wi_1 p90_wf_1=wf_1 p90_wi_2=wi_2 p90_wf_2=wf_2 (min) min_wi_1=wi_1 min_wf_1=wf_1 min_wi_2=wi_2 min_wf_2=wf_2 (max) max_wi_1=wi_1 max_wf_1=wf_1 max_wi_2=wi_2 max_wf_2=wf_2
					
					ge iter =`J'
					if iter == 1  {
						save "$matlab/W_time`yt'_eh`ed'_comp`comp'", replace
					}
					else {
						append using  "$matlab/W_time`yt'_eh`ed'_comp`comp'"
						save  "$matlab/W_time`yt'_eh`ed'_comp`comp'", replace
					}		
		
					* Transitions
								
					use if dchild0_14 == `comp' &  educationhigh == `ed' & period == `yt' using bootsample1, clear
					su  dnf_1 dni_1 dfn_1 dfi_1 din_1 dif_1 dni_df_1 dni_di_1 dnf_2 dni_2 dfn_2 dfi_2 din_2 dif_2 dni_df_2 dni_di_2	
					collapse (mean) dnf_1 dni_1 dfn_1 dfi_1 din_1 dif_1 dni_df_1 dni_di_1 dnf_2 dni_2 dfn_2 dfi_2 din_2 dif_2 dni_df_2 dni_di_2	
					ge iter =`J'
					if iter == 1  {
						save "$matlab/trans_time`yt'_eh`ed'_comp`comp'", replace
					}
					else {
						append using  "$matlab/trans_time`yt'_eh`ed'_comp`comp'"
						save  "$matlab/trans_time`yt'_eh`ed'_comp`comp'", replace
					}
					
					* Stocks - first interview
								
					use if dchild0_14 == `comp' &  educationhigh == `ed' & period == `yt' using bootsample1, clear
					su mff mfi mfn mif mnf mii min_ mni mnn
					collapse (mean) mff mfi mfn mif mnf mii min_ mni mnn
					ge iter =`J'
					if iter == 1  {
						save "$matlab/stock_time`yt'_eh`ed'_comp`comp'", replace
					}
					else {
						append using "$matlab/stock_time`yt'_eh`ed'_comp`comp'"
						save "$matlab/stock_time`yt'_eh`ed'_comp`comp'", replace
					}
					
					
			
					restore
					}
			}
	
}
local J =  `J' + 1
} /*bootstrap*/


local i = 0
foreach yt in  1 2    {					
			foreach ed in 0 1  { 
				foreach comp in 0 1  {	

				** once the bootstrap is done, convert the data files to outfiles
				use "$matlab/trans_time`yt'_eh`ed'_comp`comp'", clear
				save "$matlab/trans_time`yt'_eh`ed'_comp`comp'", replace
				drop iter 
				order dnf_1 dni_1 dfn_1 dfi_1 din_1 dif_1 dni_df_1 dni_di_1 dnf_2 dni_2 dfn_2 dfi_2 din_2 dif_2 dni_df_2 dni_di_2		
				outsheet _all using "$matlab/trans_time`yt'_eh`ed'_comp`comp'", nolabel  nonames replace

				use  "$matlab/stock_time`yt'_eh`ed'_comp`comp'", clear
				save "$matlab/stock_time`yt'_eh`ed'_comp`comp'", replace
				drop iter
				order mff mfi mfn mif mnf mii min_ mni mnn
				outsheet _all using "$matlab/stock_time`yt'_eh`ed'_comp`comp'", nolabel  nonames replace

				use "$matlab/W_time`yt'_eh`ed'_comp`comp'", clear
				save "$matlab/W_time`yt'_eh`ed'_comp`comp'", replace
				drop iter 	
				outsheet _all using "$matlab/W_time`yt'_eh`ed'_comp`comp'", nolabel  nonames replace

				}
			}
	}

*** Extração por status de saúde

local i = 0
local J = 1
local B = 100
while `J' <= `B' {
use "$savedata/sample_formodel", clear 
bsample, cluster(municip)
ge iter =`J'


save bootsample1, replace


foreach yt in  1 2  {			
	foreach gru in  0  1  {				/* any_hs */			
			foreach ed in 0 1  { 
				foreach comp in 0 1  {			/*with children in hh or not*/
					preserve
					di "educationhigh `ed' with children `comp' period `yt' any_hs `gru'"
					keep if dchild0_14 == `comp' &  educationhigh == `ed' & period == `yt' & any_hs == `gru'
					
																	
					* Wages - first interview
										
					use if dchild0_14 == `comp' &  educationhigh == `ed' & period == `yt' & any_hs == `gru' using bootsample1, clear												
					collapse (mean) mean_wi_1=wi_1 mean_wf_1=wf_1 mean_wi_2=wi_2 mean_wf_2=wf_2 (sd) sd_wi_1=wi_1 sd_wf_1=wf_1 sd_wi_2=wi_2 sd_wf_2=wf_2 /*
					*/ (p10) p10_wi_1=wi_1 p10_wf_1=wf_1 p10_wi_2=wi_2 p10_wf_2=wf_2 (p25) p25_wi_1=wi_1 p25_wf_1=wf_1 p25_wi_2=wi_2 p25_wf_2=wf_2 /*
					*/ (p50) p50_wi_1=wi_1 p50_wf_1=wf_1 p50_wi_2=wi_2 p50_wf_2=wf_2 (p75) p75_wi_1=wi_1 p75_wf_1=wf_1 p75_wi_2=wi_2 p75_wf_2=wf_2 /*
					*/ (p90) p90_wi_1=wi_1 p90_wf_1=wf_1 p90_wi_2=wi_2 p90_wf_2=wf_2 (min) min_wi_1=wi_1 min_wf_1=wf_1 min_wi_2=wi_2 min_wf_2=wf_2 (max) max_wi_1=wi_1 max_wf_1=wf_1 max_wi_2=wi_2 max_wf_2=wf_2
					
					ge iter =`J'
					if iter == 1  {
						save "$matlab/W_time`yt'_eh`ed'_comp`comp'_anyhs`gru'", replace
					}
					else {
						append using  "$matlab/W_time`yt'_eh`ed'_comp`comp'_anyhs`gru'"
						save  "$matlab/W_time`yt'_eh`ed'_comp`comp'_anyhs`gru'", replace
					}		
		
					* Transitions
								
					use if dchild0_14 == `comp' &  educationhigh == `ed' & period == `yt' & any_hs == `gru' using bootsample1, clear
					su  dnf_1 dni_1 dfn_1 dfi_1 din_1 dif_1 dni_df_1 dni_di_1 dnf_2 dni_2 dfn_2 dfi_2 din_2 dif_2 dni_df_2 dni_di_2	
					collapse (mean) dnf_1 dni_1 dfn_1 dfi_1 din_1 dif_1 dni_df_1 dni_di_1 dnf_2 dni_2 dfn_2 dfi_2 din_2 dif_2 dni_df_2 dni_di_2	
					ge iter =`J'
					if iter == 1  {
						save "$matlab/trans_time`yt'_eh`ed'_comp`comp'_anyhs`gru'", replace
					}
					else {
						append using  "$matlab/trans_time`yt'_eh`ed'_comp`comp'_anyhs`gru'"
						save  "$matlab/trans_time`yt'_eh`ed'_comp`comp'_anyhs`gru'", replace
					}
					
					* Stocks - first interview
								
					use if dchild0_14 == `comp' &  educationhigh == `ed' & period == `yt' & any_hs == `gru' using bootsample1, clear
					su mff mfi mfn mif mnf mii min_ mni mnn
					gen mfx_=mff+mfi+mfn            /*generating aggregated moments to be used only for anyhs = 1 (due to lack of size) */
					gen mix=mif+mii+min_
					collapse (mean) mff mfi mfn mif mnf mii min_ mni mnn mfx_ mix
					ge iter =`J'
					if iter == 1  {
						save "$matlab/stock_time`yt'_eh`ed'_comp`comp'_anyhs`gru'", replace
					}
					else {
						append using "$matlab/stock_time`yt'_eh`ed'_comp`comp'_anyhs`gru'"
						save "$matlab/stock_time`yt'_eh`ed'_comp`comp'_anyhs`gru'", replace
					}
					
					
			
					restore
					}
			}
	
     }
}

local J =  `J' + 1
} /*bootstrap*/


local i = 0
foreach yt in  1 2    {	
	foreach gru in  0  1  {				/* any_hs */					
			foreach ed in 0 1  { 
				foreach comp in 0 1  {	

				** once the bootstrap is done, convert the data files to outfiles
				use "$matlab/trans_time`yt'_eh`ed'_comp`comp'_anyhs`gru'", clear
				save "$matlab/trans_time`yt'_eh`ed'_comp`comp'_anyhs`gru'", replace
				drop iter 
				order dnf_1 dni_1 dfn_1 dfi_1 din_1 dif_1 dni_df_1 dni_di_1 dnf_2 dni_2 dfn_2 dfi_2 din_2 dif_2 dni_df_2 dni_di_2		
				outsheet _all using "$matlab/trans_time`yt'_eh`ed'_comp`comp'_anyhs`gru'", nolabel  nonames replace

				use  "$matlab/stock_time`yt'_eh`ed'_comp`comp'_anyhs`gru'", clear
				save "$matlab/stock_time`yt'_eh`ed'_comp`comp'_anyhs`gru'", replace
				drop iter
				order mff mfi mfn mif mnf mii min_ mni mnn mfx_ mix
				outsheet _all using "stock_time`yt'_eh`ed'_comp`comp'_anyhs`gru'", nolabel  nonames replace

				use "$matlab/W_time`yt'_eh`ed'_comp`comp'_anyhs`gru'", clear
				save "$matlab/W_time`yt'_eh`ed'_comp`comp'_anyhs`gru'", replace
				drop iter 	
				outsheet _all using "$matlab/W_time`yt'_eh`ed'_comp`comp'_anyhs`gru'", nolabel  nonames replace

				}
			}
		}
			
	}

*---------------------------------------------------------------------------------------------------
**# Matrix - health transition probabilities, and unhealthy household proportion
*---------------------------------------------------------------------------------------------------	
local i = 0
local J = 1
local B = 100
while `J' <= `B' {
use "$savedata/sample_final", clear 
bsample, cluster(municip)
ge iter =`J'


** Check de domicílios com mais ou menos do que 2 obs
// 	sort id_hh 
// 	br id_hh year quarter period time any_hs period tsince
	bysort id_hh : egen num = count(id_hh)
	tab num time, mi
	keep if num == 2 	
	drop num

* Indicador de saúde nas duas observações
	gen testa=any_hs if time==1
	gen testb=any_hs if time==2
	bysort id_hh: egen any_hs1 = max(testa)
	bysort id_hh: egen any_hs2 = max(testb)
	drop testa testb
	
* Matriz - teste	
// 	tab any_hs1 any_hs2 if educationhigh == 0 & time == 1 & dchild0_14 == 1 & period == 1, miss 
	tab any_hs1 any_hs2 if educationhigh == 1 & time == 1 & dchild0_14 == 1 & period == 1, miss 
// 	tab any_hs1 any_hs2 if educationhigh == 0 & time == 1 & dchild0_14 == 1 & period == 2, miss 
// 	tab any_hs1 any_hs2 if educationhigh == 1 & time == 1 & dchild0_14 == 1 & period == 2, miss 
	
* variáveis
	gen hea_un = 1 if any_hs1 == 0 & any_hs2 ==1
	gen un_hea = 1 if any_hs1 == 1 & any_hs2 ==0
	gen num = 1
	gen health_t = 1 if any_hs1 == 0
	gen unhealth_t = 1 if any_hs1 == 1

	
*** Extração
* Com/sem filho, educ low/hig, period antes/depois = 8 grupos/arquivos 	

foreach yt in  1  2  {					
	foreach ed in 0 1  { 
		foreach comp in 0 1  {			/*with children in hh or not*/
			preserve
			di "educationhigh `ed' with children `comp' period `yt'"
			keep if dchild0_14 == `comp' &  educationhigh == `ed' & period == `yt' & time == 1
			collapse (sum) health_t unhealth_t num hea_un un_hea
			gen phu = hea_un / health_t
			gen puh = un_hea / unhealth_t
			gen probu = unhealth_t / num
			keep phu puh probu
			
			ge iter =`J'
					if iter == 1  {
						save "$matlab/matrix_time`yt'_eh`ed'_comp`comp'", replace
					}
					else {
						append using "$matlab/matrix_time`yt'_eh`ed'_comp`comp'"
						save "$matlab/matrix_time`yt'_eh`ed'_comp`comp'", replace
					}
					
			restore			
			}	
		}
}

local J =  `J' + 1
} 

foreach yt in  1 2    {					
			foreach ed in 0 1  { 
				foreach comp in 0 1  {	

				** once the bootstrap is done, convert the data files to outfiles
				use "$matlab/matrix_time`yt'_eh`ed'_comp`comp'", clear
				save "$matlab/matrix_time`yt'_eh`ed'_comp`comp'", replace
				drop iter 	
				outsheet _all using "$matlab/matrix_time`yt'_eh`ed'_comp`comp'", nolabel  nonames replace

				}
			}
	}
	
clear


					
					
